
#include "muduo/proto/dispatcher.h"
#include "muduo/proto/codec.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpClient.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/base/CountDownLatch.h"
#include <stdio.h>
#include <unistd.h>

#include "request.pb.h"


class Client
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr; //是父类
    typedef std::shared_ptr<chen::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<chen::AddResponse> AddResponsePtr;

    Client(const std::string& ip, int port)
        :_latch(1), _client(_loopthread.startLoop(), muduo::net::InetAddress(ip, port), "Client")
        ,_dispatcher(std::bind(&Client::onUnknownMessage, this, 
                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
        ,_codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,
                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
         //注册业务请求处理函数
        _dispatcher.registerMessageCallback<chen::TranslateResponse>(std::bind(&Client::onTranslate, this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ));

        _dispatcher.registerMessageCallback<chen::AddResponse>(std::bind(&Client::onAdd, this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ));

        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, 
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setConnectionCallback(std::bind(&Client::onConnection, this, std::placeholders::_1));        
    }


    void connect()
    {
        _client.connect();
        _latch.wait(); // 阻塞等待，知道连接成功建立
    }

     //未知消息的处理请求, 1 连接，2 消息，3 时间戳
    void onUnknownMessage(const muduo::net::TcpConnectionPtr& conn,
                        const MessagePtr& message,
                        muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

    void Translate(const std::string& msg)
    {
        chen::TranslateRequest req;
        req.set_msg(msg);
        send(&req);
    }

    void Add(int num1, int num2)
    {
        chen::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        send(&req);
    }

    void onTranslate(const muduo::net::TcpConnectionPtr& conn, const TranslateResponsePtr& message, muduo::Timestamp)
    {
        std::cout << "翻译的结果是: " << message->msg() << std::endl;
    }

    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected())
        {
            _latch.countDown(); //唤醒主线程的阻塞
            _conn = conn;
        }
        else
        {   
            //连接关闭时的操作
            _conn.reset() ; 
        }
    }

    void onAdd(const muduo::net::TcpConnectionPtr& conn, const AddResponsePtr& message, muduo::Timestamp)
    {
        std::cout << "加法的结果是: " << message->result() << std::endl;
    }

    //const google::protobuf::Message 这个是所有的message的父类
    bool send(const google::protobuf::Message* msg)
    {
        if(_conn->connected())
        {
            _codec.send(_conn, *msg);
            return true;
        }
        return false;
    }

private:
    muduo::CountDownLatch _latch; //实现同步的
    muduo::net::EventLoopThread _loopthread; //异步循环处理线程
    muduo::net::TcpConnectionPtr _conn; //客户端对应的连接
    muduo::net::TcpClient _client;  //发起请求的客户端
    ProtobufDispatcher _dispatcher; //请求分发器
    ProtobufCodec _codec; //协议处理器
};


int main()
{
    Client client("127.0.0.1", 8085);
    client.connect();

    client.Translate("gg");
    client.Add(10, 15);
    sleep(1);
    return 0;
}